﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model;
using System.Data;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Enterprise.RelationQuery
{
    public partial class DeliveryAreaManagement : BasePage
    {
        public DeliveryAreaManagement()
        {
            this.ModuleNo = Module.配送区域管理.ToString();
        }

        IBLL_Area iArea = Factory.BLL_Area();
        IBLL_SelectArea iSelectArea = Factory.BLL_SelectArea();
        IBLL_SelectRelation iSelectRelation = Factory.BLL_SelectRelation();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                setBind();
            }
        }


        /// <summary>
        /// 绑定所有符合条件的采购目录
        /// </summary>
        private void setBind()
        {

            Model.Area aArea = CurrentAccount.GetCurrentDeployArea();  //获取当前实施地区的地区

            IList<Model.Area> aAreaList = iArea.GetAllCity(aArea.AreaID);
            gvwArea.DataSource = aAreaList;
            gvwArea.DataBind();

            IList<Model.SelectArea> sSelectAreaList = iSelectArea.GetAllCity(CurrentAccount.GetUser().Org.OrgID);

            IList<Model.SelectRelation> sSelectRelationList = iSelectRelation.GetAllSelectRelationByCompanyPSID(CurrentAccount.GetUser().Org.OrgID);

            string strPSAreas = "";
            foreach (Model.SelectRelation item in sSelectRelationList)
            {
                if (strPSAreas.Contains(item.AreaID) == false)
                {
                    strPSAreas += item.AreaID + "'";
                }
            }
            if (sSelectAreaList.Count > 0)
            {
                hidStatus.Value = "1";
                for (int _index = 0; _index < gvwArea.Rows.Count; _index++)
                {
                    CheckBox ckb = (CheckBox)gvwArea.Rows[_index].FindControl("checkbox1");
                    if (sSelectAreaList[0].AreaIDs.Contains(this.gvwArea.DataKeys[_index].Value.ToString().ToUpper()))
                    {
                        ckb.Checked = true;

                        if (strPSAreas.Contains(this.gvwArea.DataKeys[_index].Value.ToString().ToUpper()))
                        {
                            ckb.Enabled = false;
                        }
                    }
                }
            }
            else
            {
                hidStatus.Value = "0";
            }
        }

        /// <summary>
        /// 将所选区域加入自身自身可配送区域中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            SelectArea sArea = new SelectArea();
            //获取所有选择的目录
            string ids = "";

            //遍历列表的所有行
            for (int _index = 0; _index < gvwArea.Rows.Count; _index++)
            {
                CheckBox ckb = ((CheckBox)gvwArea.Rows[_index].FindControl("checkbox1"));
                if (ckb.Checked)
                {
                    ids += this.gvwArea.DataKeys[_index].Value.ToString() + ",";
                }
            }

            if (!string.IsNullOrEmpty(ids))
            {
                sArea.AreaIDs = ids;
                sArea.CompanyID_PS = CurrentAccount.GetUser().Org.OrgID;


                //调取方法 
                if (this.hidStatus.Value == "1")
                {
                    if (iSelectArea.UpdateSelectArea(sArea) == 1)
                    {
                        setBind();
                    }
                    else
                    {
                        PageError("保存失败！", this.ReturnUrl);
                    }
                }
                else
                {
                    if (iSelectArea.AddSelectArea(sArea) == 1)
                    {
                        setBind();
                    }
                    else
                    {
                        PageError("保存失败！", this.ReturnUrl);
                    }
                }
            }
            else
            {
                HZ.Web.WebUtil.Alert("请至少选定一个配送区域！", this);
            }
        }
    }
}
